package com.pb.b2b.model.enums;

import java.sql.Types;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.pb.b2b.common.enums.EnumarationType;

public class UserType extends EnumarationType {
	private static final long serialVersionUID = 1L;

	public static final UserType ADMINISTRATOR = new UserType("A", "ADMINISTRATOR");
	public static final UserType USER = new UserType("U", "USER");
	public static final UserType CUSTOMER = new UserType("C", "CUSTOMER");

	protected static ArrayList<UserType> list = new ArrayList<UserType>();

	static {
		// Only Types.VARCHAR and Types.INTEGER can be used!
		SQL_TYPES = new int[] { Types.VARCHAR };
		list.add(ADMINISTRATOR);
		list.add(USER);
		list.add(CUSTOMER);
	}

	public UserType() {
	}

	@Override
	public List<UserType> fetchList() {
		return list;
	}

	public List<UserType> getList() {
		return list;
	}

	public UserType(String value, String label) {
		super(value, label);
	}

	public static UserType getById(String id) {
		Iterator<UserType> it = list.iterator();
		while (it.hasNext()) {
			UserType entity = it.next();
			String entityId = entity.getValue();
			if (entityId != null && entityId.equalsIgnoreCase(id))
				return entity;
		}
		return null;
	}

	public static UserType getByName(String name) {
		Iterator<UserType> it = list.iterator();
		while (it.hasNext()) {
			UserType entity = it.next();
			String entityName = entity.getLabel();
			if (entityName.equalsIgnoreCase(name))
				return entity;
		}
		return null;
	}
}
